<template>
  <div>
    <el-breadcrumb separator-class='el-icon-arrow-right'>
      <el-breadcrumb-item v-bind:to='{ path: "/" }'>首页</el-breadcrumb-item>
      <el-breadcrumb-item>新闻管理</el-breadcrumb-item>
      <el-breadcrumb-item>发布新闻资讯</el-breadcrumb-item>
    </el-breadcrumb>
    <div class='dict-main'>
      <el-tabs v-model='activeName' class='title-top'>
        <el-tab-pane label='发布新闻资讯' name='first'>
          <div class='main-module charger'>
            <el-row class='posre'>
              <el-col :span='12' class='text-right search-top'>
                <el-button size='small' type="primary" icon="el-icon-check" @click="submitForm('ruleForm')"> 保 存
                </el-button>
                <el-button size='small' type='warning' icon='el-icon-back' @click='backToList()'>返回</el-button>
              </el-col>
            </el-row>
            <div>
              <el-row :gutter="20">
                <el-col :span="14" :offset="2">
                  <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" label-suffix=":">
                    <el-form-item label="标题名称" prop="title">
                      <el-input v-model="addForm.title"></el-input>
                    </el-form-item>
                    <el-form-item label="副标题名称" prop="subTitle">
                      <el-input v-model="addForm.subTitle"></el-input>
                    </el-form-item>
                    <el-form-item label="发布时间" prop="publishTime">
                      <el-date-picker
                        v-model="addForm.publishTime"
                        type="datetime"
                        format="yyyy-MM-dd HH:mm:ss"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="width: 50%;"
                        placeholder="发布时间选择">
                      </el-date-picker>
                    </el-form-item>
                    <el-form-item label='Banner图片' prop='bannerImage' >
                      <el-upload
                        class='avatar-uploader'
                        action=''
                        :show-file-list='false'
                        :http-request='doUploadBanner'
                        :before-upload='beforeAvatarUpload'>
                        <img v-if='addForm.bannerImage' :v-model='addForm.bannerImage' :src='addForm.showBannerImage' class='avatar' >
                        <i v-else class='el-icon-plus avatar-uploader-icon'>Banner图片</i>
                      </el-upload>
                    </el-form-item>
                    <el-form-item label='封面图片' prop='coverImage' >
                      <el-upload
                        class='avatar-uploader'
                        action=''
                        :show-file-list='false'
                        :http-request='doUploadCover'
                        :before-upload='beforeAvatarUpload'>
                        <img v-if='addForm.coverImage' :v-model='addForm.coverImage' :src='addForm.showCoverImage' class='avatar' >
                        <i v-else class='el-icon-plus avatar-uploader-icon'>封面图片</i>
                      </el-upload>
                    </el-form-item>
                    <el-form-item label="新闻内容" prop="content" style="margin-bottom: 100px;width: 150%;">
                      <quill-editor v-model="addForm.content" style="height: 400px;" ref="myQuillEditor"
                                    :options="editorOption"
                                    @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
                                    @ready="onEditorReady($event)">
                      </quill-editor>
                    </el-form-item>
                  </el-form>
                </el-col>
              </el-row>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>


  </div>
</template>

<script>
  import {
    addImageText
  } from '@/api/imageText';
  import { uploadOss } from '@/api/image-oss';
  import { getOSSProperties } from '@/api/image';
  export default {
    data() {
      return {
        ossProperties: {},
        baseDir: 'news/image-text',
        activeName: 'first',
        editorOption: {
          modules: {
            toolbar: [
              ['bold', 'italic', 'underline', 'strike', 'blockquote'],        // toggled buttons
              [{'header': 1}, {'header': 2}],
              [{'size': ['small', false, 'large', 'huge']}],
              [{'color': []}, {'background': []}],
              [{'list': 'ordered'}, {'list': 'bullet'}],
              [{'indent': '-1'}, {'indent': '+1'}],
              ['link', 'image'],
              ['clean']
            ]
          }
        },
        addForm: {
          title: '',
          subTitle: '',
          publishTime: '',
          content: '',
          coverImage: '',
          bannerImage: ''
        },
        rules: {
          title: [
            {required: true, message: '请输入标题名称', trigger: 'blur'},
            {min: 3, max: 150, message: '长度在 3 到 150 个字符', trigger: 'blur'}
          ],
          subTitle: [
            {required: true, message: '请输入副标题名称', trigger: 'blur'},
            {min: 3, max: 150, message: '长度在 3 到 150 个字符', trigger: 'blur'}
          ],
          publishTime: [
            {required: true, message: '请填写生效日期', trigger: 'blur'}
          ],
          bannerImage: [
            {required: true, message: '请上传Banner图', trigger: 'blur'}
          ],
          coverImage: [
            {required: true, message: '请上传封面图', trigger: 'blur'}
          ],
          content: [
            {required: true, message: '请填写图文内容', trigger: 'blur'}
          ]
        }
      };
    },
    filters: {},
    methods: {
      onEditorBlur(quill) {
        console.log('editor blur!', quill)
      },
      onEditorFocus(quill) {
        console.log('editor focus!', quill)
      },
      onEditorReady(quill) {
        console.log('editor ready!', quill)
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      backToList() {
        this.$router.push({
          path: '/news/imageText'
        })
      },
      beforeAvatarUpload (file) {
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 10;

        if (!isJPG) {
          this.$message.error('上传logo图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传logo图片大小不能超过 10MB!');
        }
        return isJPG && isLt2M;
      },
      doUploadCover (file) {
        var _this = this;
        uploadOss(_this.ossProperties, file, function (url) {
          _this.addForm.coverImage=url;
          _this.addForm.showCoverImage=_this.ossProperties.showUrl+url;
        });
      },
      doUploadBanner (file) {
        var _this = this;
        uploadOss(_this.ossProperties, file, function (url) {
          _this.addForm.bannerImage=url;
          _this.addForm.showBannerImage=_this.ossProperties.showUrl+url;
        });
      },
      submitForm() {
        var that = this;
        that.$refs.addForm.validate((valid) => {
          if (valid) {
            addImageText(that.addForm).then(response => {
              if (response.code == 0) {
                that.$message({
                  message: '发布图文信息成功',
                  type: 'success'
                });
                that.backToList();
              } else {
                that.$message({
                  message: response.message,
                  type: 'error'
                });
              }
            });
          } else {
            return false;
          }
        })
      }
    },
    watch: {
    },
    created: function () {
      getOSSProperties(this.baseDir).then(response => {
        if(response.code == 0){
          this.ossProperties = response.data;
        }else{
          this.$message({
            message: response.message,
            type: 'error'
          });
        }
      });
    }
  };
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    float: left;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    /*font-size: 28px;*/
    color: #8c939d;
    width: 133px;
    height: 133px;
    line-height: 133px;
    text-align: center;
  }
  .avatar {
    width: 133px;
    height: 133px;
    display: block;
  }
  .time {
    font-size: 13px;
    color: #999;
  }

  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .button {
    padding: 0;
    float: right;
  }

  .image {
    width: 100%;
    display: block;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both
  }
</style>
